package com.wbt.img;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * @program: Thread-demo
 * @description:
 * @author: Wang·Baotong
 * @create: 2021-05-26 16:57
 */
public class PictureUtils {

    private static Logger log = LoggerFactory.getLogger(PictureUtils.class);

    /**
     * 缩放图片
     *
     * @param image     原图片，不能为空
     * @param zoomLevel 缩放倍数，不能小于0
     * @return 返回缩放后的图片
     */
    public static BufferedImage zoom(BufferedImage image, float zoomLevel) {

        int newImageWidth = (int) (image.getWidth() * zoomLevel);
        int newImageHeight = (int) (image.getHeight() * zoomLevel);

        BufferedImage newImage = new BufferedImage(newImageWidth, newImageHeight, image.getType());
        Graphics2D g = newImage.createGraphics();
        g.drawImage(image, 0, 0, newImageWidth, newImageHeight, null);
        g.dispose();

        return newImage;
    }

    /**
     * 缩放图片
     *
     * @param image     原图片，不能为空
     * @param newImageWidth 宽
     * @param newImageHeight 高
     * @return 返回缩放后的图片
     */
    public static BufferedImage zoom(BufferedImage image, int newImageWidth,int newImageHeight ) {


        BufferedImage newImage = new BufferedImage(newImageWidth, newImageHeight, image.getType());
        Graphics2D g = newImage.createGraphics();
        g.drawImage(image, 0, 0, newImageWidth, newImageHeight, null);
        g.dispose();

        return newImage;
    }



    /**
     * 写入图片到指定位置
     *
     * @param image
     * @param filepath
     * @param extension
     * @throws IOException
     */
    public static File writeImage(BufferedImage image, String filepath, String extension) throws IOException {

        File file = new File(filepath);
        ImageIO.write(image, extension, file);
        return file;
    }


    public static BufferedImage thumbnailImage(File srcImg, int w, int h){
        if(srcImg.exists()){
            BufferedImage bi = null;
            try {
                // ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]
                String types = Arrays.toString(ImageIO.getReaderFormatNames()).replace("]", ",");
                String suffix = null;
                // 获取图片后缀
                if(srcImg.getName().indexOf(".") > -1) {
                    suffix = srcImg.getName().substring(srcImg.getName().lastIndexOf(".") + 1);
                }// 类型和图片后缀全部小写，然后判断后缀是否合法
                if(suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()+",") < 0){
                    log.error("Sorry, the image suffix is illegal. the standard image suffix is {}." + types);
                    return null;
                }
                log.debug("target image's size, width:{}, height:{}.",w,h);
                Image img = ImageIO.read(srcImg);

                bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.getGraphics();
                g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
                g.dispose();
                // 将图片保存在原目录并加上前缀
                //ImageIO.write(bi, suffix, output);
                //output.close();

            } catch (IOException e) {
                log.error("generate thumbnail image failed.",e);
            }
            return bi;
        }else{
            log.warn("the src image is not exist.");
            return null;
        }
    }
}
